package com.rfchina.okr.xianren.controller.api;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.rfchina.okr.xianren.base.BaseNoLoginController;
import com.rfchina.okr.xianren.model.ActivityRegion;
import com.rfchina.okr.xianren.model.AreaInfo;
import com.rfchina.okr.xianren.model.CategoryInfo;
import com.rfchina.okr.xianren.model.RechargeInfo;
import com.rfchina.okr.xianren.model.UserMoney;
import com.rfchina.okr.xianren.model.WithdrawInfo;
import com.rfchina.okr.xianren.util.DateTimeUtil;
import com.rfchina.okr.xianren.util.MoneyObjectType;
import com.rfchina.okr.xianren.util.SqlUtil;

public class PayInfoController extends BaseNoLoginController {

	// 我的充值列表
	public void my_recharge_info_list() {
		Long uid = loginUid();

		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("size", 50);
		// int
		String sql = "from recharge_info where uid=" + uid;
		List<Object> where = new ArrayList<Object>();
		Date begin_create_time = getParaToDate("begin_create_time");
		Date end_create_time = getParaToDate("end_create_time");
		Integer status = getParaToInt("status");
		sql = SqlUtil.integer(sql, status, "status", where);
		if (begin_create_time != null) {
			begin_create_time = DateTimeUtil.setTime(begin_create_time, 0, 0, 0);
		}
		if (end_create_time != null) {
			end_create_time = DateTimeUtil.setDate235959(end_create_time);
		}
		sql = SqlUtil.date(sql, begin_create_time, end_create_time, "create_time", where);

		sql += " order  by id desc ";

		String select = "select * ";
		Page<Record> pager = Db.paginate(pageNumber, pageSize, select, sql, where.toArray());

		// for (Record object : pager.getList()) {
		// object.set("object_type_title",
		// MoneyObjectType.valueOf(object.getStr("object_type")));
		// }

		myRenderJson(pager);
	}

	// 我的资金流水列表

	public void my_money_flow_list() {
		Long uid = loginUid();

		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("size", 50);
		// int
		String sql = "from money_flow where uid=" + uid;
		List<Object> where = new ArrayList<Object>();
		Date begin_create_time = getParaToDate("begin_create_time");
		Date end_create_time = getParaToDate("end_create_time");
		if (begin_create_time != null) {
			begin_create_time = DateTimeUtil.setTime(begin_create_time, 0, 0, 0);
		}
		if (end_create_time != null) {
			end_create_time = DateTimeUtil.setDate235959(end_create_time);
		}
		sql = SqlUtil.date(sql, begin_create_time, end_create_time, "create_time", where);

		sql += " order  by id desc ";

		String select = "select * ";
		Page<Record> pager = Db.paginate(pageNumber, pageSize, select, sql, where.toArray());

		for (Record object : pager.getList()) {
			object.set("object_type_title", MoneyObjectType.valueOf(object.getStr("object_type")));
		}

		myRenderJson(pager);

	}

	// 我的提现列表
	public void my_withdraw_info_list() {
		Long uid = loginUid();

		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("size", 50);
		// int
		String sql = "from withdraw_info where uid=" + uid;
		List<Object> where = new ArrayList<Object>();
		Date begin_create_time = getParaToDate("begin_create_time");
		Date end_create_time = getParaToDate("end_create_time");
		Integer status = getParaToInt("status");
		sql = SqlUtil.integer(sql, status, "status", where);
		if (begin_create_time != null) {
			begin_create_time = DateTimeUtil.setTime(begin_create_time, 0, 0, 0);
		}
		if (end_create_time != null) {
			end_create_time = DateTimeUtil.setDate235959(end_create_time);
		}
		sql = SqlUtil.date(sql, begin_create_time, end_create_time, "create_time", where);

		sql += " order  by id desc ";

		String select = "select * ";
		Page<Record> pager = Db.paginate(pageNumber, pageSize, select, sql, where.toArray());

		// for (Record object : pager.getList()) {
		// object.set("object_type_title",
		// MoneyObjectType.valueOf(object.getStr("object_type")));
		// }

		myRenderJson(pager);
	}

	// 创建充值单
	public void create_recharge_info() {
		Long uid = loginUid();
		String amountStr = getPara("amount");
		BigDecimal amount = new BigDecimal(amountStr);
		String payType = getPara("pay_type");
		RechargeInfo ri = new RechargeInfo();
		ri.setUid(uid);
		ri.setAppid(getAppid());
		ri.setCreateTime(new Date());
		ri.setAmount(amount);
		// 状态 1支付中,2支付成功,3支付超时
		Integer status = 1;
		ri.setStatus(status);
		ri.save();
		myRenderJsonSuccess("支付成功,请稍等");
	}

	// 发起提现单
	public void create_withdraw_info() {
		Long uid = loginUid();
		String amountStr = getPara("amount", "0");
		String bank_username = getPara("bank_username");
		String bank_name = getPara("bank_name");
		String bank_no = getPara("bank_no");
		verification(StrKit.isBlank(bank_no), "银行卡号不能为空");
		verification(StrKit.isBlank(bank_username), "姓名不能为空");
		verification(StrKit.isBlank(bank_name), "支行全称名称不能为空");
		BigDecimal amount = new BigDecimal(amountStr);
		verification(new BigDecimal("0.01").compareTo(amount) == 1, "必须大于1分");
		UserMoney mm = UserMoney.dao.findById(uid);
		verification(mm.getMoney().compareTo(amount) == -1, "必须大于自己的余额 " + mm.getMoney() + " 元");

		WithdrawInfo wi = new WithdrawInfo();
		wi.setUid(uid);
		wi.setAppid(getAppid());
		wi.setCreateTime(new Date());
		wi.setStatus(1);
		wi.setAmount(amount);
		wi.setBankUsername(bank_username);
		wi.setBankName(bank_name);
		wi.setBankNo(bank_no);
		wi.save();
		myRenderJsonSuccess("提现创建,请稍等审批");
	}

}
